/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.IsfdMeshCompareTask;
import com.comac.ins.isfd.domain.bo.IsfdMeshCompareQueryBo;
import com.comac.ins.isfd.service.IIsfdMeshCompareService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 自然网格模型对比
 *
 * @author lj
 * @date 2025-01-013
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/mesh/compare")
public class IsfdMeshCompareController extends BaseController {

    @Autowired
    private IIsfdMeshCompareService compareService;

    /**
     * 新增模型对比任务
     */
    @SaCheckPermission("mesh:compare:add")
    @Log(title = "新增模型对比任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> addTask(@Validated @RequestBody IsfdMeshCompareTask task) {
        return toAjax(compareService.createCompareTask(task));
    }

    /**
     * 模型对比任务列表查询
     */
    @SaCheckPermission("mesh:compare:list")
    @PostMapping("/list")
    public TableDataInfo<Document> queryTaskList(@Validated @RequestBody IsfdMeshCompareQueryBo queryBo) {
        return compareService.queryList(queryBo);
    }

    /**
     * 模型对比结果查询
     */
    @SaCheckPermission("mesh:compare:detail")
    @GetMapping("/result/detail")
    public R<Document> getResult(@RequestParam("_id") String id) {
        return R.ok(compareService.queryResultDetail(id));
    }

    /**
     * 删除任务及任务结果
     *
     * @param id 主键
     */
    @SaCheckPermission("mesh:compare:remove")
    @Log(title = "【删除任务及任务结果】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String id) {
        return toAjax(compareService.deleteCompareTask(id));
    }

    /**
     * 模型对比结果导出
     */
    @SaCheckPermission("mesh:compare:report")
    @RepeatSubmit()
    @GetMapping("/result/report")
    public void report(@RequestParam("_id") String id, HttpServletResponse response) {
        compareService.report(id, response);
    }

    /**
     * 模型对比结果导出excel的base64格式
     */
    @SaCheckPermission("mesh:compare:report")
    @RepeatSubmit()
    @GetMapping("/result/base64/report")
    public R<String> base64Report(@RequestParam("_id") String id) {
        return R.ok(compareService.base64Report(id));
    }
}
